﻿// Decompiled with JetBrains decompiler
// Type: DnProcessSimulateCommands.ContinuousMacro.CApCMContinuousMacroUtilities
// Assembly: DnProcessSimulateCommands, Version=2402.0.0.890, Culture=neutral, PublicKeyToken=null
// MVID: A885D881-AEDF-447E-852A-6F13102E089C
// Assembly location: C:\Program Files\Tecnomatix_2402\eMPower\DotNetCommands\DnProcessSimulateCommands.dll

using EngineeringInternalExtension;
using System;
using System.Collections.ObjectModel;
using Tecnomatix.Engineering;


namespace DnProcessSimulateCommands.ContinuousMacro;

internal static class CApCMContinuousMacroUtilities
{
  internal static TxRoboticViaLocationOperation GetSelectedViaLocation(
    Func<TxRoboticViaLocationOperation, bool> selectionCheck)
  {
    TxSelection activeSelection = TxApplication.ActiveSelection;
    if (activeSelection.Count == 1)
    {
      TxRoboticViaLocationOperation selectedViaLocation = ((Collection<ITxObject>) activeSelection.GetItems())[0] as TxRoboticViaLocationOperation;
      if (TxRoboticViaLocationOperation.op_Inequality(selectedViaLocation, (ITxObject) null) && selectedViaLocation.ParentRoboticOperation is ITxContinuousOperation && selectionCheck(selectedViaLocation))
        return selectedViaLocation;
    }
    return (TxRoboticViaLocationOperation) null;
  }

  internal static void OrientViewToLocation(TxTransformation location)
  {
    if (!TxTransformation.op_Inequality(location, (TxTransformation) null))
      return;
    if (!(TxApplication.ViewersManager.GetActiveViewer() is ITxGraphicDisplayer graphicDisplayer))
      graphicDisplayer = (ITxGraphicDisplayer) TxApplication.ViewersManager.GraphicViewer;
    TxVector axis = TxTransformationEx.GetAxis(location, (TxTransformationEx.TxAxisEx) 1);
    axis.Normalize();
    TxVector txVector1 = TxVector.op_UnaryNegation(TxTransformationEx.GetAxis(location, (TxTransformationEx.TxAxisEx) 2));
    txVector1.Normalize();
    TxCamera currentCamera = graphicDisplayer.CurrentCamera;
    TxVector txVector2 = TxVector.op_Subtraction(currentCamera.ReferencePoint, currentCamera.Position);
    double num = Math.Sqrt(TxVector.DotProduct(txVector2, txVector2));
    TxVector txVector3 = TxVector.op_Addition(location.Translation, TxVector.op_Multiply(txVector1, num));
    graphicDisplayer.CurrentCamera = new TxCamera(location.Translation, txVector3, axis);
    TxApplication.RefreshDisplay();
  }

  internal static void OrientViewToLocation(TxTransformation location, bool isFlipped)
  {
    if (!TxTransformation.op_Inequality(location, (TxTransformation) null))
      return;
    if (isFlipped)
      CApCMContinuousMacroUtilities.OrientViewToLocation(CApCMContinuousMacroUtilities.FlipZAxis(location));
    else
      CApCMContinuousMacroUtilities.OrientViewToLocation(location);
  }

  internal static TxTransformation FlipZAxis(TxTransformation location)
  {
    TxTransformation txTransformation = new TxTransformation(new TxVector(Math.PI, 0.0, 0.0), (TxTransformation.TxRotationType) 1);
    return TxTransformation.op_Multiply(location, txTransformation);
  }

  internal static TxComponent CreateDisplayParent()
  {
    using (TxLocalComponentCreationData componentCreationData = new TxLocalComponentCreationData())
    {
      componentCreationData.SetAsDisplay();
      return TxApplication.ActiveDocument.PhysicalRoot.CreateLocalComponent(componentCreationData);
    }
  }

  internal static TxObjectList GetComponents(this ITxObjectCollection part)
  {
    switch (part)
    {
      case TxComponent _:
        TxObjectList components = new TxObjectList(1);
        ((Collection<ITxObject>) components).Add((ITxObject) part);
        return components;
      case TxCompoundPart _:
        return part.GetAllDescendants((ITxTypeFilter) new TxTypeFilter(typeof (TxComponent)));
      default:
        return new TxObjectList();
    }
  }
}
